/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RecursosView.java
 *
 * Created on Oct 29, 2011, 11:02:34 AM
 */
package praticav.view;

import javax.swing.JOptionPane;
import org.hibernate.HibernateException;
import org.hibernate.exception.ConstraintViolationException;
import praticav.dao.RecursosDAO;
import praticav.model.Recursos;

/**
 *
 * @author Jonatah Benatti
 */
public class RecursosView extends javax.swing.JFrame {

    int cod;
    String desc;
    String tipoRe;
    double valor;
    int conta;
    
    /** Creates new form RecursosView */
    public RecursosView() {
        conta=0;
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        txtDescricaoRecurso = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        cbxTipoRecurso = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        txtValorUnitario = new javax.swing.JTextField();
        btnSalvar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Descrição do Recurso"));

        txtDescricaoRecurso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDescricaoRecursoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtDescricaoRecurso, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(txtDescricaoRecurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Tipo do Recurso"));

        cbxTipoRecurso.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Material", "Horas" }));
        cbxTipoRecurso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxTipoRecursoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cbxTipoRecurso, 0, 167, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(cbxTipoRecurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Valor Unitário"));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtValorUnitario, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(txtValorUnitario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/praticav/images/save.png"))); // NOI18N
        btnSalvar.setText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/praticav/images/close.png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 218, Short.MAX_VALUE)
                        .addComponent(btnCancelar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalvar)
                    .addComponent(btnCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public RecursosView(int codigo, String descricao, String tipo, double valorunitario) {
        try {
            cod = codigo;
            desc = descricao;
            tipoRe = tipo;
            valor = valorunitario;
            conta=1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        initComponents();
        txtDescricaoRecurso.setText(""+desc);
        cbxTipoRecurso.setSelectedItem(""+tipoRe);
        txtValorUnitario.setText(""+valor);
    }
    
    private void txtDescricaoRecursoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDescricaoRecursoActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtDescricaoRecursoActionPerformed

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        // Salvar
      if (txtDescricaoRecurso.getText().equals(""))
      {
          JOptionPane.showMessageDialog(null, "O campo DESCRIÇÃO deve estar preenchido!", "Atenção", WIDTH);
      } else
      {
          if (conta==0)
          {
            Recursos f = new Recursos();
            f.setRecdescricao(txtDescricaoRecurso.getText());
            f.setRectipo(cbxTipoRecurso.getSelectedItem().toString());
            
            try {
                f.setRecvalorunitario(Double.parseDouble(txtValorUnitario.getText()));
                RecursosDAO dao = new RecursosDAO();
            
                try{
                    dao.inserir(f);
                } catch(ConstraintViolationException ce){
                    JOptionPane.showMessageDialog(null, "Erro ao gravar os dados!!!  "+ ce.getMessage());
                } catch (HibernateException ex){
                    JOptionPane.showMessageDialog(null, "Erro: "+ex.getMessage());
                }

                txtDescricaoRecurso.setText("");
                this.hide();
                
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "O campo VALOR UNITÁRIO deve receber somente números!" +e.getMessage(), "Atenção", WIDTH);
                txtValorUnitario.setText("");
            }
          } 
          else
          {
            Recursos f = new Recursos();
            f.setReccodigo(cod);
            f.setRecdescricao(txtDescricaoRecurso.getText());
            f.setRectipo(cbxTipoRecurso.getSelectedItem().toString());
            try{
                f.setRecvalorunitario(Double.parseDouble(txtValorUnitario.getText()));
                RecursosDAO dao = new RecursosDAO();

                try{
                    dao.editar(f);
                } catch(ConstraintViolationException ce){
                    JOptionPane.showMessageDialog(null, "Erro ao modificar os dados!!!  "+ ce.getMessage());
                } catch (HibernateException ex){
                    JOptionPane.showMessageDialog(null, "Erro: "+ex.getMessage());
                }

                txtDescricaoRecurso.setText("");
                this.hide();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "O campo VALOR UNITÁRIO deve receber somente números!" +e.getMessage(), "Atenção", WIDTH);
                txtValorUnitario.setText("");
            }
          }
      }  
}//GEN-LAST:event_btnSalvarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // Cancelar
        this.hide();
}//GEN-LAST:event_btnCancelarActionPerformed

    private void cbxTipoRecursoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxTipoRecursoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbxTipoRecursoActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        if (conta == 0)
        {
            this.setTitle("Cadastro de Recursos");
        }else
        {
            this.setTitle("Alteração de Recursos");
        }
    }//GEN-LAST:event_formWindowOpened

    /**
     * @param args the command line arguments
     */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JComboBox cbxTipoRecurso;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField txtDescricaoRecurso;
    private javax.swing.JTextField txtValorUnitario;
    // End of variables declaration//GEN-END:variables
}
